<?php
header("content-type:text/html;charset=utf-8");
date_default_timezone_set('PRC');  //设置中国时区
require("config.php");
$sign = 0;
$title = $_POST['title'];
$time = time();
$author = $_POST['author'];
$content = $_POST['content'];
$insert = "INSERT INTO article(title,time,author,content) VALUES ('$title','$time','$author','$content')";
$result = mysql_query($insert);
if(mysql_affected_rows()>0)
{
	$sign = 1;
}
else
{
	echo '<script>alert("失败");location.href="column.php"</script>';
}

if($sign == 1 && !empty($_FILES['pic']['tmp_name']))
{
$select = "SELECT id FROM article WHERE title='$title'";
$result = mysql_query($select);
$row = mysql_fetch_array($result);
$id = $row['id'];

$allowtype = array("gif","png","jpg");									//可接受的文件类型
$size = 1000000;														//文件大小
$path = "./pic";														//保存路径

//判断文件是否可以成功上传到服务器，$FILES["pic"]["error"]为0表示上传成功！
if ($_FILES["pic"]["error"]>0)
{
	echo "上传错误：";
	switch ($_FILES["pic"]["error"])
	{
		case 1: die("上传文件大小超出了PHP配置文件中的约定值：upload_max_filesize");
		case 2: die("上传文件大小超出了表单中的约定值：MAX_FILE_SIZE");
		case 3: die("文件只有部分上传");
		case 4: die("没有上传任何文件");
		case 6: die("找不到临时文件夹");
		default : die("未知错误");
	}
}

//判断上传的文件是否为允许的文件类型，通过文件的后缀名
$en = explode(".",$_FILES["pic"]["name"]);
$hz = array_pop($en);
if(!in_array($hz,$allowtype))
{
	die("这个后缀是<b>{$hz}</b>，不是允许的文件类型");
}
if($_FILES["pic"]["size"] > $size)
{
	die("超过了允许的<b>{$size}</b>字节大小");
}

$filename = date("YmdHis").rand(100,999).".".$hz;

if(is_uploaded_file($_FILES["pic"]["tmp_name"]))
{
	if(!move_uploaded_file($_FILES["pic"]["tmp_name"],$path."/".$filename))
	{
		die("问题：不能讲文件移动到指定目录！");
	}
}
else
{
	die('问题：上传文件($_FILES["pic"]["name"])不是一个合法文件');
}



$pd = "/cms/images/".$filename;

$insertpic = "INSERT INTO picture(type,url) VALUES('$hz','$pd');";
$resultpic = mysql_query($insertpic);

if($resultpic)
{
	echo "<script>alert('文章上传成功！');location.href='column.php'</script>";
}
}
else
{
	echo "<script>alert('文章上传成功！');location.href='column.php'</script>";
}

if($sign==1)
{
mysql_free_result($result);
}
my_colse($con);
?>